<!-- 在线图片转url网站https://pixabay.com/zh/illustrations/search/%e5%8a%a8%e6%bc%ab/ -->
<template>
  <div>
    <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      multiple
      :limit="3"
      :on-exceed="handleExceed"
      :file-list="fileList1"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传jpg/png文件，且不超过500kb
      </div>
    </el-upload>
    <el-upload
      action="https://jsonplaceholder.typicode.com/posts/"
      :http-request="uploadFile"
      list-type="picture-card"
      drag
      :file-list="fileList"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      fileList1: [],
      fileList: [{ name: '1', url: 'https://cdn.pixabay.com/photo/2024/02/18/13/13/ai-generated-8581189_1280.jpg' }, { name: '2', url: 'https://cdn.pixabay.com/photo/2024/03/03/12/42/ai-generated-8610368_1280.png' }, { name: '3', url: 'https://cdn.pixabay.com/photo/2023/07/31/13/42/woman-8161029_1280.png' }],
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    uploadFile (f) {
      this.$http.jsonp('https://jsonplaceholder.typicode.com/posts/',
        {
          headers: {},
          emulateJSON: true
        }).then((response) => { console.log('"success"') })
        .catch(() => { console.log('"error"') })
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file);
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 下载文件
    downloadFile () {
      // 文件的URL地址
      const fileUrl = 'https://www.irs.gov/pub/irs-pdf/fw4.pdf?spm=5176.28103460.0.0.104f5d27RtPLhQ&file=fw4.pdf'
      // 下载文件
      window.open(fileUrl)
      this.importDialogVisible = false
    },

  }
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
